package site.jlopen.kuscia.rpc.impl;

import java.io.FileNotFoundException;

import org.secretflow.v1alpha1.factory.KusciaAPIChannelFactory;
import org.secretflow.v1alpha1.kusciaapi.Serving.BatchQueryServingStatusRequest;
import org.secretflow.v1alpha1.kusciaapi.Serving.BatchQueryServingStatusResponse;
import org.secretflow.v1alpha1.kusciaapi.Serving.CreateServingRequest;
import org.secretflow.v1alpha1.kusciaapi.Serving.CreateServingResponse;
import org.secretflow.v1alpha1.kusciaapi.Serving.DeleteServingRequest;
import org.secretflow.v1alpha1.kusciaapi.Serving.DeleteServingResponse;
import org.secretflow.v1alpha1.kusciaapi.Serving.QueryServingRequest;
import org.secretflow.v1alpha1.kusciaapi.Serving.QueryServingResponse;
import org.secretflow.v1alpha1.kusciaapi.Serving.UpdateServingRequest;
import org.secretflow.v1alpha1.kusciaapi.Serving.UpdateServingResponse;
import org.secretflow.v1alpha1.kusciaapi.ServingServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.ServingServiceGrpc.ServingServiceBlockingStub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import site.jlopen.kuscia.rpc.KusciaServingRpc;
import site.jlopen.kuscia.exception.SdkBaseException;
import site.jlopen.kuscia.properties.KusciaAPIProperties;

import io.grpc.ManagedChannel;

public class KusciaServingRpcImpl implements KusciaServingRpc {
	private static Logger logger = LoggerFactory.getLogger(KusciaServingRpcImpl.class);

	private ServingServiceBlockingStub blockingStub;
	private ManagedChannel channel;

	public KusciaServingRpcImpl(KusciaAPIProperties properties) {
		try {
			KusciaAPIChannelFactory channelFactory = new KusciaAPIChannelFactory(properties.getAddress(),
					properties.getTokenFile(), properties.getTls(), properties.getProtocol());
			this.channel = channelFactory.newClientChannel();
			this.blockingStub = ServingServiceGrpc.newBlockingStub(channel);
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				throw SdkBaseException.of("证书文件不存在", e);
			}
			throw SdkBaseException.of("Channel通讯异常", e);
		}
	}

	@Override
	public CreateServingResponse createServing(CreateServingRequest request) {
		try {
			CreateServingResponse response = blockingStub.createServing(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public UpdateServingResponse updateServing(UpdateServingRequest request) {
		try {
			UpdateServingResponse response = blockingStub.updateServing(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public DeleteServingResponse deleteServing(DeleteServingRequest request) {
		try {
			DeleteServingResponse response = blockingStub.deleteServing(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public QueryServingResponse queryServing(QueryServingRequest request) {
		try {
			QueryServingResponse response = blockingStub.queryServing(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public BatchQueryServingStatusResponse batchQueryServingStatus(BatchQueryServingStatusRequest request) {
		try {
			BatchQueryServingStatusResponse response = blockingStub.batchQueryServingStatus(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

}
